using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace whereIsTheDonutGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Gate : Microsoft.Xna.Framework.DrawableGameComponent, Solid
    {
        donutGame game;
        private Model gate;
        private ICamera camera;
        float trans = 0;
        private Trigger t;
        int rotation;
        public BoundingBox bbox;
        private int x, z;
        public Gate(donutGame game, Trigger t,int x, int z, int r)
            : base(game)
        {
            this.game = game;
            this.t = t;
            this.x = x;
            this.z = z;
            this.rotation = r;
            this.bbox = new BoundingBox(new Vector3(x - 4, game.terrain.Height[x, z] - 1, -z - 0.5f), new Vector3(x + 4, game.terrain.Height[x, z] + 3, -z + 0.5f));
        }


        
        public override void Initialize()
        {
            camera = (ICamera)Game.Services.GetService(typeof(ICamera));
            base.Initialize();
        }

        public List<BoundingBox> getBoundingBoxes()
        {
            List<BoundingBox> boxes = new List<BoundingBox>();
            boxes.Add(bbox);
            return boxes;
        }

        public override void Update(GameTime gameTime)
        {
            if (t.triggered && trans < 15)
            {
                trans += 0.1f;
            }
            if (!t.triggered && trans > 0)
            {
                trans -= 0.05f;
            }
            UpdateBoundingBox();
            base.Update(gameTime);
        }


        private void UpdateBoundingBox()
        {
            this.bbox = new BoundingBox(new Vector3(x - 10, game.terrain.Height[x, z] - 1 + trans, -z - 10), new Vector3(x + 10, game.terrain.Height[x, z] + 3 + trans, -z + 10));
        }
        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            foreach (ModelMesh mesh in gate.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.View = camera.ViewMatrix;
                    effect.Projection = camera.ProjectionMatrix;
                    effect.World = Matrix.CreateTranslation(new Vector3(x, game.terrain.Height[x, z] + trans, -z));
                }
                mesh.Draw();
            }
            base.Draw(gameTime);
        }

        protected override void LoadContent()
        {
            gate = this.Game.Content.Load<Model>("cage");
        }
    }
}
